// echo partial file, should not compile along

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;
using HarmonyLib;
using Utils;
namespace GongfaEffect;

using GameData.Domains;

public partial class GongfaEffect {
//! 这是一个专门针对太吾的Mod，这个Mod允许太吾在一定程度上操纵NPC的过月行为
    public static class LuanQingZhang_Old {
        public static sbyte DefKey_MakeLove=Config.PersonalNeed.DefKey.MakeLove;
        public static ushort RelationType_Adored=GameData.Domains.Character.Relation.RelationType.Adored;
        public const short Defkey_LuanQingZhang=289; // 目前这个方法并没有被暴露在defkey之外，因此只能如此使用。
        public static void Patch(RobustHarmonyInstance HarmonyInstance, string ModIdStr){
            DefKey_MakeLove=(sbyte)typeof(Config.PersonalNeed.DefKey).GetField("MakeLove",(BindingFlags)(-1)).GetValue(null);
            RelationType_Adored=(ushort)typeof(GameData.Domains.Character.Relation.RelationType).GetField("Adored",(BindingFlags)(-1)).GetValue(null);
            HarmonyInstance.PatchAll(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType);
        }
        //public static mlList=new List<int>();
        //public static newmlList=new List<int>();
        [HarmonyPrefix]
        [HarmonyPatch(typeof(GameData.Domains.World.WorldDomain),"PeriAdvanceMonth_CharacterFixedAction")]
        public static void Prefix(GameData.Domains.World.WorldDomain __instance, GameData.Common.DataContext context, int areaId){
            // 春宵需求代码：
            // GameData.Domains.Character.Ai.PersonalNeed need = GameData.Domains.Character.Ai.PersonalNeed.CreatePersonalNeed(23, targetCharId);
            // selfChar.AddPersonalNeed(context, need);

            //    Span<GameData.Domains.Map.MapBlockData> blocks = GameData.Domains.DomainManager.Map.GetAreaBlocks((short)areaId);
            //    GameData.Domains.Map.MapBlockData blockData = *blocks[i];
            //    HashSet<int> charIds = blockData.CharacterSet;
            //    bool isTaiwuBlock = taiwuLocation.AreaId == blockData.AreaId && taiwuLocation.BlockId == blockData.BlockId;
            var taiwu=GameData.Domains.DomainManager.Taiwu.GetTaiwu();
            var loc=taiwu.GetLocation();
            if(areaId!=loc.AreaId)return;
            var twid=taiwu.GetId();
            GameData.Domains.CombatSkill.CombatSkill cs;
            if(GameData.Domains.DomainManager.CombatSkill.TryGetElement_CombatSkills(new GameData.Domains.CombatSkill.CombatSkillKey(twid, Defkey_LuanQingZhang),out cs) && cs.GetDirection() > GameData.Domains.CombatSkill.CombatSkillDirection.None) {
                HashSet<int> charIds=null;
                int repeats;
                if(cs.GetDirection()==GameData.Domains.CombatSkill.CombatSkillDirection.Direct){
                    charIds=GameData.Domains.DomainManager.Taiwu.GetGroupCharIds().GetCollection();
                    repeats=5;
                } else {
                    try{
                         charIds=GameData.Domains.DomainManager.Map.GetBlock(loc).CharacterSet;
                    }finally{repeats=3;}
                }
                if(charIds!=null){
                    GameData.Domains.Character.Ai.PersonalNeed need = GameData.Domains.Character.Ai.PersonalNeed.CreatePersonalNeed(DefKey_MakeLove, twid);
                    foreach(var charId in charIds){
                        if(charId!=twid){
                            GameData.Domains.Character.Character character;
                            if(GameData.Domains.DomainManager.Character.TryGetElement_Objects(charId,out character)){
                                var nm=character.GetFullName().GetName(character.GetGender(),GameData.Domains.DomainManager.World.GetCustomTexts());
                                GameData.Domains.Character.Relation.RelatedCharacter relation;
                                if(GameData.Domains.DomainManager.Character.TryGetRelation(charId, twid, out relation) && ((relation.RelationType & RelationType_Adored) !=0)){
                                    GameData.Utilities.AdaptableLog.Info(nm.Item1+nm.Item2+"与太吾已经是爱慕关系，只向"+nm.Item1+nm.Item2+"("+character.GetId().ToString()+")"+"增添春宵需求，");
                                } else {
                                    GameData.Utilities.AdaptableLog.Info(nm.Item1+nm.Item2+"与太吾不是爱慕关系，向"+nm.Item1+nm.Item2+"("+character.GetId().ToString()+")"+"增添爱慕与春宵需求，");
                                    //GameData.Domains.DomainManager.Character.ChangeRelationType(context, charId, twid,  RelationType_Adored , 0);
                                    //context.ParallelModificationsRecorder.RecordType(GameData.Common.ParallelModificationType.PeriAdvanceMonthRelationsUpdate);
                                    //context.ParallelModificationsRecorder.RecordParameterClass<GameData.Domains.Character.Character>(character);
                                }
                                for(int i=0;i<repeats;i++)character.AddPersonalNeed(context, need);
                            }
                        }
                    }
                }
            }
        }
    }
}
